package com.sinosoft.product;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.base.Constants;
import com.sinosoft.base.GenericManagerImpl;

@Service("productSearchManager")
public class ProductSearchManagerImp extends GenericManagerImpl<ZywxImage1, Long> implements ProductSearchManager{
	
	ProductSearchDao productDao;
	
	@Autowired
	public ProductSearchManagerImp(ProductSearchDao productDao){
		super(productDao);
		this.productDao = productDao;
	}
	
	private String genSql4ProductSearch(HashMap queryParams){
		String productLevel = (String)queryParams.get("productLevel");
		StringBuffer sb = new StringBuffer("select * from ");
		sb.append(productLevel).append(" where 1=1");
		String sensors = (String)queryParams.get("sensors");
		if(!StringUtils.isEmpty(sensors)){
			String[] sensor = sensors.split(",");
			if(sensor.length>1){
				sb.append(" and (");
				for(int i=0;i<sensor.length;i++){
					String[] satAndSensor = sensor[i].split("_");
					if(i==0){
						sb.append("(satelliteid='")
						  .append(satAndSensor[0])
						  .append("' and sensorid='")
						  .append(satAndSensor[1])
						  .append("')");
					}
					else{
						sb.append(" or (satelliteid='")
						  .append(satAndSensor[0])
						  .append("' and sensorid='")
						  .append(satAndSensor[1])
						  .append("')");
					}
				}
				sb.append(")");
			}
			else{
				String[] satAndSensor = sensor[0].split("_");
				sb.append(" and satelliteid='")
				  .append(satAndSensor[0])
				  .append("' and sensorid='")
				  .append(satAndSensor[1])
				  .append("'");
			}
		}
		String stations = (String)queryParams.get("stations");
		if(!StringUtils.isEmpty(stations)){
			String[] stationItem = stations.split("_");
			if(stationItem.length>1){
				sb.append(" and (");
				for(int i=0;i<stationItem.length;i++){
					String stationName = stationItem[i];
					if(i==0){
						sb.append("receivestationid='")
						  .append(stationName)
						  .append("'");
					}
					else{
						sb.append(" or receivestationid='")
						  .append(stationName)
						  .append("'");
					}
				}
				sb.append(")");
			}
			else{
				String stationName = stationItem[0];
				sb.append(" and receivestationid='")
				  .append(stationName)
				  .append("'");
			}
		}
		//all level's query condition except level0
		if(!"ZYWX_IMAGE_0".equalsIgnoreCase(productLevel)){
			String colStartDate = (String)queryParams.get("colStartDate");
			if(!StringUtils.isEmpty(colStartDate)){
				sb.append(" and receivetime >")
				  .append(genDateQueryStr(colStartDate));
			}
			String colEndDate = (String)queryParams.get("colEndDate");
			if(!StringUtils.isEmpty(colEndDate)){
				sb.append(" and receivetime <")
				  .append(genDateQueryStr(colEndDate));
			}
			String prodStartDate = (String)queryParams.get("prodStartDate");
			if(!StringUtils.isEmpty(prodStartDate)){
				sb.append(" and productdate >")
				  .append(genDateQueryStr(prodStartDate));
			}
			String prodEndDate = (String)queryParams.get("prodEndDate");
			if(!StringUtils.isEmpty(prodEndDate)){
				sb.append(" and productdate <")
				  .append(genDateQueryStr(prodEndDate));
			}
			String minCC = (String)queryParams.get("minCC");
			if(!StringUtils.isEmpty(minCC)){
				sb.append(" and cloudpercent >")
				  .append(minCC);
			}
			String maxCC = (String)queryParams.get("maxCC");
			if(!StringUtils.isEmpty(maxCC)){
				sb.append(" and cloudpercent <")
				  .append(maxCC);
			}
		}
		
		//level0's query condition
		else{
			String colStartDate = (String)queryParams.get("colStartDate");
			if(!StringUtils.isEmpty(colStartDate)){
				sb.append(" and SCENEDATE >")
				  .append(genDateQueryStr(colStartDate));
			}
			String colEndDate = (String)queryParams.get("colEndDate");
			if(!StringUtils.isEmpty(colEndDate)){
				sb.append(" and SCENEDATE <")
				  .append(genDateQueryStr(colEndDate));
			}
			//生产时间对0级不生效
//			String prodStartDate = (String)queryParams.get("prodStartDate");
//			if(!StringUtils.isEmpty(prodStartDate)){
//				sb.append(" and productdate >")
//				  .append(genDateQueryStr(prodStartDate));
//			}
//			String prodEndDate = (String)queryParams.get("prodEndDate");
//			if(!StringUtils.isEmpty(prodEndDate)){
//				sb.append(" and productdate <")
//				  .append(genDateQueryStr(prodEndDate));
//			}
			String minCC = (String)queryParams.get("minCC");
			if(!StringUtils.isEmpty(minCC)){
				sb.append(" and CLOUDCOVERQUOTE >")
				  .append(minCC);
			}
			String maxCC = (String)queryParams.get("maxCC");
			if(!StringUtils.isEmpty(maxCC)){
				sb.append(" and CLOUDCOVERQUOTE <")
				  .append(maxCC);
			}
		}
		
		
		sb.append(" and rownum between 1 and 10");
		return sb.toString();
	}
	
	private String genSql4SpecialSearch(HashMap queryParams){
		String specialType = (String)queryParams.get("sType");
		StringBuffer sb = new StringBuffer("select * from ");
		sb.append(specialType).append(" where 1=1");		
		String productid = (String)queryParams.get("productId");
		if(!StringUtils.isEmpty(productid)){
			sb.append(" and productid=")		
			.append(productid);
		}		
		String prodStartDate = (String)queryParams.get("prodStartDate");
		if(!StringUtils.isEmpty(prodStartDate)){
			sb.append(" and productdate >")
			  .append(genDateQueryStr(prodStartDate));
		 }
		String prodEndDate = (String)queryParams.get("prodEndDate");
	    if(!StringUtils.isEmpty(prodEndDate)){
			sb.append(" and productdate <")
			  .append(genDateQueryStr(prodEndDate));
		}
		
		sb.append(" and rownum between 1 and 10");
		return sb.toString();
	 }
	

	private String genDateQueryStr(String dateStr){
		return "to_date('" + dateStr + "', 'yyyy-MM-dd')";
	}
	
	public String doProductSearch(final HashMap queryParams){
		String productLevel = (String)queryParams.get("productLevel");
		if("ZYWX_IMAGE_0".equalsIgnoreCase(productLevel)){
			return productDao.doSearchLevel0(genSql4ProductSearch(queryParams));
		}
		else{
			return productDao.doProductSearch(genSql4ProductSearch(queryParams));
		}
	}
	
	/**
	 * 查询元数据
	 * @param productlevel
	 * @param satelliteid
	 * @param sensorid
	 * @param productid
	 * @param sceneid
	 * @param modelname
	 * @return
	 */
	public Object getZywxImageModel(String productlevel,String satelliteid,String sensorid,String productid,String sceneid,String modelname)
	{
		StringBuffer sb = new StringBuffer("from "+modelname);
		sb.append(" where 1=1");
		if(productlevel.equalsIgnoreCase(Constants.LEVEL0_SCENE))
		{
			sb.append(" and sceneid="+sceneid);
		}
		else
		{
			sb.append(" and productid="+productid);
		}
		sb.append(" and satelliteid='"+satelliteid+"'");
		sb.append(" and sensorid='"+sensorid+"'");
		return productDao.getZywxImageModel(sb.toString());
	}
	
	public String doSpecialSearch(final HashMap queryParams){
		return productDao.doSpecialSearch(genSql4SpecialSearch(queryParams));
	}
	
	public Object getMetaDataById(final String levelAndId){
		return productDao.getMetaDataById(levelAndId);
	}
	
	public List<Object> getProducts(final String level, final String keys){
		return  productDao.getProducts(level, keys);
	}
	
	/**
	 * 元数据列表
	 * @param hql
	 * @return
	 */
	public List<Object> getZywxImageList(String hql,int maxsize)
	{
		return productDao.getZywxImageList(hql,maxsize);
	}
	
	/**
	 * 过滤元数据
	 */
	public String ProductFilterManager(String ids,String ordername,String tablename)
	{
		StringBuffer sql = new StringBuffer("select ");
		if(tablename.equalsIgnoreCase(Constants.LEVEL_0_TABLE))
			sql.append("sceneid");
		else
			sql.append("productid");
		sql.append(" from ");
		sql.append(tablename);
		sql.append(" where (");
		sql.append(ordername);
		sql.append(",scenepath,scenerow) in(select max("); 
		sql.append(ordername);
		sql.append("),t.scenepath,t.scenerow from ");
		sql.append(tablename);
		sql.append(" t group by t.scenepath,t.scenerow)");
		List list = new ArrayList();
		String[] idstrings = ids.split(",");
		for (int i = 0; i < idstrings.length; i++) {
			if(i==0)
			{
				sql.append(" and (productid="+idstrings[i]);
			}
			else if((idstrings.length-1)==i)
			{
				sql.append(" or productid="+idstrings[i]+")");
			}
			else
			{
				sql.append(" or productid="+idstrings[i]);
			}
		}
		
		List productlist = productDao.ProductFilterDao(sql.toString());
		String result = "";
		for (int i = 0; i < productlist.size(); i++) {
			if((productlist.size()-1)==i)
			{
				result = result + productlist.get(i);
			}
			else
			{
				result = result + productlist.get(i)+",";
			}
		}
		return result;
	}
}
